package com.ruoyi.web.controller.warning;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.warning.domain.entity.Sensor;
import com.ruoyi.warning.domain.query.SensorQuery;
import com.ruoyi.warning.service.ISensorsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenhc
 * @since 2025-03-25
 */
@RestController
@RequestMapping("/sensors")
@Api(tags = "传感器", description = "用于处理传感器相关操作的控制器")
public class SensorsController {
    @Resource
    private ISensorsService sensorsService;
    @ApiOperation("获取所有传感器")
    @GetMapping(value = "/getAllSensors")
    public R getAllSensors(SensorQuery query)
    {
        return R.ok(sensorsService.getAllSensors(query));
    }

    @ApiOperation("传感器详情")
    @GetMapping(value = "/getSensorsDetail/{id}")
    public R getSensorsDetail(@PathVariable String id)
    {
        return R.ok(sensorsService.getById(id));
    }

    @ApiOperation("修改传感器")
    @PostMapping(value = "/updateSensors")
    public R updateSensors(@RequestBody Sensor sensor)
    {
        return R.ok(sensorsService.updateById(sensor));
    }

    @ApiOperation("设置主温度传感器")
    @GetMapping(value = "/setMainTempSensor/{id}")
    public R setMainTempSensor(@PathVariable String id)
    {
        Sensor sensor = sensorsService.getById(id);
        //找到所有的温度，然后sort设置为0
        List<Sensor> list = sensorsService.list(new LambdaQueryWrapper<Sensor>()
                .eq(Sensor::getDeviceId,sensor.getDeviceId())
                .eq(Sensor::getSensorClass,"temperature"));
        // 将所有传感器sort设为0
        list.forEach(sensor1 -> sensor1.setSort(0));
        sensor.setSort(1);
        list.add(sensor);
        sensorsService.updateBatchById(list);
        return R.ok();
    }
}
